package com.wyw.learning.thread.lock.lock;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用tryLock来避免死锁
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * @date 2021/7/14   10:16 下午
 * <p>
 * Update History:
 * Author        Time            Content
 */
public class TryLockDeadlock implements Runnable {
    int flag = 1;
    static Lock lock1 = new ReentrantLock();
    static Lock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        TryLockDeadlock r1 = new TryLockDeadlock();
        TryLockDeadlock r2 = new TryLockDeadlock();

        r1.flag = 0;
        r2.flag = 1;

        new Thread(r1).start();
        new Thread(r2).start();
    }
    @Override
    public void run() {
        for(int i= 0; i<100; i++){
            if(flag == 1){
                try {
                    if (lock1.tryLock(800, TimeUnit.MILLISECONDS)){
                        try{
                            System.out.println("线程1获取了锁1");
                            Thread.sleep(new Random().nextInt(1000));

                            if (lock2.tryLock(800, TimeUnit.MILLISECONDS)){
                                try{
                                    System.out.println("线程1获取了锁2");
                                    System.out.println("线程1成功获得两把锁");
                                    break;
                                } finally {
                                    lock2.unlock();
                                }
                            } else{
                                System.out.println("线程1获取了锁2失败，已经重试");
                            }

                        } finally {
                            lock1.unlock();
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    } else{
                        System.out.println("线程1获取了锁1失败，已经重试");
                    }
                } catch (InterruptedException e) {//期间锁可能会被中断
                    e.printStackTrace();
                }
            }
            if(flag == 0){
                try {
                    if (lock2.tryLock(3000, TimeUnit.MILLISECONDS)){
                        try{
                            System.out.println("线程2获取了锁2");
                            Thread.sleep(new Random().nextInt(1000));

                            if (lock1.tryLock(800, TimeUnit.MILLISECONDS)){
                                try{
                                    System.out.println("线程2获取了锁1");
                                    System.out.println("线程2成功获得两把锁");
                                    break;
                                } finally {
                                    lock1.unlock();
                                }
                            } else{
                                System.out.println("线程2获取了锁1失败，已经重试");
                            }

                        } finally {
                            lock2.unlock();
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    } else{
                        System.out.println("线程2获取了锁2失败，已经重试");
                    }
                } catch (InterruptedException e) {//期间锁可能会被中断
                    e.printStackTrace();
                }
            }
        }

    }
}
